package com.iflytek.springboot_medicine.service;

import com.iflytek.springboot_medicine.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
    /**
     * 根据名字分页查询用户（如果名字为空，则查询所有用户）
     *
     * @param name     用户名（可选）
     * @param pageable 分页信息
     * @return 分页结果
     */
    @Query("SELECT u FROM User u WHERE (:name IS NULL OR u.name LIKE CONCAT('%', COALESCE(:name, ''), '%'))")
    Page<User> findByName(@Param("name") String name, Pageable pageable);

    User deleteById(int id);

    User findByUsernameAndPassword(String username, String password);

    User findByName(String username);

    User findByUsername(String username);

}
